package tenant

import (
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/model"
	"errors"
	"go.mongodb.org/mongo-driver/bson"
)

type TenantInfoRepository interface {
	base.Repository
}

func NewTenantInfoRepository() TenantInfoRepository {
	return &tenantInfoRepository{
		base.RepositoryStruct{
			CollectionName: server.TenantInfo,
		},
	}
}

type tenantInfoRepository struct {
	base.RepositoryStruct
}

func (t *tenantInfoRepository) Insert(mdl *model.DataUserModel) error {
	if info, ok := mdl.Data.(*TenantInfo); ok {
		if err := t.CheckDB(); err != nil {
			return err
		}
		info.ResolveEntity(info, mdl.User)
		_, err := t.Mongo.InsertOne(t.CollectionName, info)
		return err
	} else {
		return model.NewTypeError("tenantInfo")
	}
}

func (t *tenantInfoRepository) Update(mdl *model.DataUserModel) error {
	if info, ok := mdl.Data.(*TenantInfo); ok {
		if err := t.CheckDB(); err != nil {
			return err
		}
		filter := bson.M{
			"tenantId": info.TenantId,
		}
		info.ResolveFilter(info, mdl.User, &filter)
		set := bson.M{}
		//accountSet := bson.M{}
		if info.Account.Version != "" {
			set["account.version"] = info.Account.Version
		}
		if !info.Account.BeginDate.IsZero() {
			set["account.beginDate"] = info.Account.BeginDate
		}
		if !info.Account.EndDate.IsZero() {
			set["account.endDate"] = info.Account.EndDate
		}
		if info.Account.IsTrial != nil {
			set["account.isTrial"] = info.Account.IsTrial
		}
		if info.Account.RemainingDays != 0 {
			set["account.remainingDays"] = info.Account.RemainingDays
		}

		if info.Organization.Name != "" {
			set["organization.name"] = info.Organization.Name
		}
		if info.Organization.Logo != "" {
			set["organization.logo"] = info.Organization.Logo
		}
		if info.Organization.Industry != "" {
			set["organization.industry"] = info.Organization.Industry
		}
		if info.Organization.Location != "" {
			set["organization.location"] = info.Organization.Location
		}
		if info.Organization.Workplace != "" {
			set["organization.workplace"] = info.Organization.Workplace
		}
		if info.Organization.Ico != "" {
			set["organization.ico"] = info.Organization.Ico
		}
		if info.Organization.DomainRegistrationInfo != "" {
			set["organization.domainRegistrationInfo"] = info.Organization.DomainRegistrationInfo
		}

		info.ResolveUpdateCondition(info, mdl.User, &set)
		update := bson.M{
			"$set": set,
		}
		_, err := t.Mongo.UpdateOne(t.CollectionName, filter, update)
		return err
	} else {
		return model.NewTypeError("tenantInfo")
	}
}

// 根据租户id删除
func (t *tenantInfoRepository) DeleteLogic(mdl *model.DataUserModel) error {
	if info, ok := mdl.Data.(*TenantInfo); ok {
		if info.TenantId == "" {
			return errors.New("tenantId is required.")
		}
		if err := t.CheckDB(); err != nil {
			return err
		}
		filter := bson.M{
			"tenantId": info.TenantId,
		}
		info.ResolveFilter(info, mdl.User, &filter)
		updateOpt := bson.M{}
		info.DeleteLogic(info, mdl.User, &updateOpt)
		update := bson.M{
			"$set": updateOpt,
		}
		_, err := t.Mongo.UpdateOne(t.CollectionName, filter, update)
		return err
	} else {
		return model.NewTypeError("tenantInfo")
	}
}

func (t *tenantInfoRepository) Find(mdl *model.DataUserModel) (interface{}, error) {
	if info, ok := mdl.Data.(*TenantInfo); ok {
		if err := t.CheckDB(); err != nil {
			return nil, err
		}
		filter := bson.M{}
		info.ResolveFilter(info, mdl.User, &filter)
		result := []*TenantInfo{}
		err := t.Mongo.FindMany(t.CollectionName, filter, result)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		return nil, model.NewTypeError("tenantInfo")
	}
}

func (t *tenantInfoRepository) FindOne(mdl *model.DataUserModel) (interface{}, error) {
	if info, ok := mdl.Data.(*TenantInfo); ok {
		if err := t.CheckDB(); err != nil {
			return nil, err
		}
		filter := bson.M{
			"id": info.Id,
		}
		if mdl.Filter != nil {
			filter = *mdl.Filter
		}
		info.ResolveFilter(info, mdl.User, &filter)
		result := &TenantInfo{}
		find, err := t.Mongo.FindOne(t.CollectionName, filter, result)
		if find {
			return result, nil
		}
		return nil, err
	} else {
		return nil, model.NewTypeError("tenantInfo")
	}
}
